🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / ui / src / commonMain / kotlin / org / meshtastic / core / ui / util / LocalEventBranding.kt
Displaying Raw • Download
core/ui/src/commonMain/kotlin/org/meshtastic/core/ui/util/LocalEventBranding.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 8.92 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ui.util
Tff7b72import T7ee787androidx.compose.foundation.Image
Tff7b72import T7ee787androidx.compose.runtime.Composable
Tff7b72import T7ee787androidx.compose.runtime.compositionLocalOf
Tff7b72import T7ee787androidx.compose.ui.Modifier
Tff7b72import T7ee787androidx.compose.ui.graphics.Color
Tff7b72import T7ee787androidx.compose.ui.graphics.vector.rememberVectorPainter
Tff7b72import T7ee787androidx.compose.ui.layout.ContentScale
Tff7b72import T7ee787coil3.compose.AsyncImage
Tff7b72import T7ee787kotlinx.datetime.LocalDate
Tff7b72import T7ee787kotlinx.datetime.TimeZone
Tff7b72import T7ee787kotlinx.datetime.todayIn
Tff7b72import T7ee787org.jetbrains.compose.resources.DrawableResource
Tff7b72import T7ee787org.jetbrains.compose.resources.painterResource
Tff7b72import T7ee787org.jetbrains.compose.resources.vectorResource
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareEdition
Tff7b72import T7ee787org.meshtastic.core.model.EventFirmwareLink
Tff7b72import T7ee787org.meshtastic.core.resources.Res
Tff7b72import T7ee787org.meshtastic.core.resources.ic_meshtastic
Tff7b72import T7ee787org.meshtastic.core.resources.img_event_defcon
Tff7b72import T7ee787org.meshtastic.core.resources.img_event_hamvention
Tff7b72import T7ee787kotlin.time.Clock
T8b949e/**
* Provides the active [EventFirmwareEdition] (if any) to the composition tree. When a connected device reports an event
* firmware edition, this local is populated at the app root so that
* [MainAppBar][org.meshtastic.core.ui.component.MainAppBar] can display event branding automatically — no per-screen
* wiring needed.
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffCompositionLocalAllowlistTa5d6ff"Tb4b4b4)
Tff7b72val Te6edf3LocalEventBranding Tff7b72= Te6edf3compositionLocalOfTff7b72<Te6edf3EventFirmwareEdition?Tff7b72> Tb4b4b4{ Tff7b72null Tb4b4b4}
T8b949e/**
* Bundled branding drawable for an edition, or `null`. Used as the offline fallback by [EventBrandingIcon] when a
* hosted [EventFirmwareEdition.iconUrl] is absent or fails to load.
*/
Tff7b72fun Td2a8ffeventIconForTb4b4b4(Te6edf3editionNameTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3DrawableResource? Tff7b72= Tff7b72when Tb4b4b4(Te6edf3editionNameTb4b4b4) Tb4b4b4{
Ta5d6ff"Ta5d6ffHAMVENTIONTa5d6ff" Tff7b72-Tff7b72> Te6edf3ResTb4b4b4.Te6edf3drawableTb4b4b4.Te6edf3img_event_hamvention
Ta5d6ff"Ta5d6ffDEFCONTa5d6ff" Tff7b72-Tff7b72> Te6edf3ResTb4b4b4.Te6edf3drawableTb4b4b4.Te6edf3img_event_defcon
Tff7b72else Tff7b72-Tff7b72> Tff7b72null
Tb4b4b4}
T8b949e/**
* Whether [url] is safe to fetch or open from event metadata: an absolute `https` URL whose authority is a valid host
* with an optional numeric port.
*
* The manifest is first-party but arrives over the network, and its URLs reach an image loader and the platform URI
* handler. A URI handler will honour whatever scheme it is given, so without this check a bad or tampered manifest
* entry could invoke arbitrary handlers on the device. Scheme comparison is case-insensitive; everything else is
* rejected, including protocol-relative (`//host`) and scheme-relative input.
*
* The authority is matched whole rather than merely tested for emptiness, so a port with no host (`https://:443`),
* whitespace, a truncated IPv6 literal, and userinfo (`https://trusted.host@evil.example`, which wears a trusted
* prefix) are all rejected — none of those characters appear in the permitted set.
*/
Tff7b72fun Td2a8ffsafeBrandUrlOrNullTb4b4b4(Te6edf3urlTb4b4b4: Tffa657String?Tb4b4b4)Tb4b4b4: Tffa657String? Tb4b4b4{
Tff7b72val Te6edf3trimmed Tff7b72= Te6edf3urlTff7b72?.Te6edf3trimTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3orEmptyTb4b4b4(Tb4b4b4)
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3trimmedTb4b4b4.Te6edf3startsWithTb4b4b4(Te6edf3HTTPS_SCHEMETb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4)Tb4b4b4) Tff7b72return Tff7b72null
Tff7b72val Te6edf3authority Tff7b72= Te6edf3trimmedTb4b4b4.Te6edf3dropTb4b4b4(Te6edf3HTTPS_SCHEMETb4b4b4.Te6edf3lengthTb4b4b4)Tb4b4b4.Te6edf3takeWhile Tb4b4b4{ Tffa657it Tff7b72!Tff7b72= Ta5d6ff'/' Tff7b72&Tff7b72& Tffa657it Tff7b72!Tff7b72= Ta5d6ff'?' Tff7b72&Tff7b72& Tffa657it Tff7b72!Tff7b72= Ta5d6ff'#' Tb4b4b4}
T8b949e// Return the trimmed form, not the caller's original: consumers must use the string that was actually validated,
T8b949e// or a padded-but-otherwise-valid URL passes here and then fails as a malformed URI at the image loader.
Tff7b72return Te6edf3trimmedTb4b4b4.Te6edf3takeIf Tb4b4b4{ Te6edf3AUTHORITY_REGEXTb4b4b4.Te6edf3matchesTb4b4b4(Te6edf3authorityTb4b4b4) Tb4b4b4}
Tb4b4b4}
T8b949e/** Whether [url] is safe to fetch or open — see [safeBrandUrlOrNull], which callers should prefer for the value. */
Tff7b72fun Td2a8ffisSafeBrandUrlTb4b4b4(Te6edf3urlTb4b4b4: Tffa657String?Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72= Te6edf3safeBrandUrlOrNullTb4b4b4(Te6edf3urlTb4b4b4) Tff7b72!Tff7b72= Tff7b72null
T8b949e/**
* Links that are safe to open, carrying the normalized URL — see [safeBrandUrlOrNull]. Unsafe or blank entries are
* dropped rather than rendered and refused on tap.
*/
Tff7b72fun Te6edf3EventFirmwareEditionTb4b4b4.Td2a8ffsafeLinksTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3EventFirmwareLinkTff7b72> Tff7b72=
Te6edf3linksTb4b4b4.Te6edf3mapNotNull Tb4b4b4{ Te6edf3link Tff7b72-Tff7b72> Te6edf3safeBrandUrlOrNullTb4b4b4(Te6edf3linkTb4b4b4.Te6edf3urlTb4b4b4)Tff7b72?.Te6edf3let Tb4b4b4{ Te6edf3linkTb4b4b4.Te6edf3copyTb4b4b4(Te6edf3url Tff7b72= Tffa657itTb4b4b4) Tb4b4b4} Tb4b4b4}
T8b949e/**
* Event branding icon: loads the hosted [EventFirmwareEdition.iconUrl] when present *and* safe to fetch, falling back
* to the bundled per-edition drawable ([eventIconFor]), and finally the Meshtastic logo. The fallback painter also
* backs Coil's loading/error states so there is never an empty slot.
*/
Tf0883e@Composable
Tff7b72fun Td2a8ffEventBrandingIconTb4b4b4(
Te6edf3editionTb4b4b4: Te6edf3EventFirmwareEditionTb4b4b4,
Te6edf3modifierTb4b4b4: Te6edf3Modifier Tff7b72= Te6edf3ModifierTb4b4b4,
Te6edf3contentDescriptionTb4b4b4: Tffa657String? Tff7b72= Te6edf3editionTb4b4b4.Te6edf3displayNameTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3bundled Tff7b72= Te6edf3eventIconForTb4b4b4(Te6edf3editionTb4b4b4.Te6edf3editionTb4b4b4)
Tff7b72val Te6edf3fallback Tff7b72=
Te6edf3bundledTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3painterResourceTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4} Tff7b72?: Te6edf3rememberVectorPainterTb4b4b4(Te6edf3vectorResourceTb4b4b4(Te6edf3ResTb4b4b4.Te6edf3drawableTb4b4b4.Te6edf3ic_meshtasticTb4b4b4)Tb4b4b4)
Tff7b72val Te6edf3url Tff7b72= Te6edf3safeBrandUrlOrNullTb4b4b4(Te6edf3editionTb4b4b4.Te6edf3iconUrlTb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3urlTb4b4b4.Te6edf3isNullOrBlankTb4b4b4(Tb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3ImageTb4b4b4(
Te6edf3painter Tff7b72= Te6edf3fallbackTb4b4b4,
Te6edf3contentDescription Tff7b72= Te6edf3contentDescriptionTb4b4b4,
Te6edf3contentScale Tff7b72= Te6edf3ContentScaleTb4b4b4.Te6edf3FitTb4b4b4,
Te6edf3modifier Tff7b72= Te6edf3modifierTb4b4b4,
Tb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3AsyncImageTb4b4b4(
Te6edf3model Tff7b72= Te6edf3urlTb4b4b4,
Te6edf3contentDescription Tff7b72= Te6edf3contentDescriptionTb4b4b4,
Te6edf3modifier Tff7b72= Te6edf3modifierTb4b4b4,
Te6edf3contentScale Tff7b72= Te6edf3ContentScaleTb4b4b4.Te6edf3FitTb4b4b4,
Te6edf3placeholder Tff7b72= Te6edf3fallbackTb4b4b4,
Te6edf3error Tff7b72= Te6edf3fallbackTb4b4b4,
Te6edf3fallback Tff7b72= Te6edf3fallbackTb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
T8b949e/**
* Whether the event's last day is in the past, evaluated in the event's own [timeZone][EventFirmwareEdition.timeZone]
* (falling back to the device time zone). Used to nudge users off event firmware once the event is over. Returns
* `false` when [eventEnd][EventFirmwareEdition.eventEnd] is absent or unparseable — an unknown end date is never
* treated as ended.
*/
Tff7b72fun Te6edf3EventFirmwareEditionTb4b4b4.Td2a8ffhasEndedTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72val Te6edf3end Tff7b72= Te6edf3eventEndTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3runCatching Tb4b4b4{ Te6edf3LocalDateTb4b4b4.Te6edf3parseTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4) Tb4b4b4} Tff7b72?: Tff7b72return Tff7b72false
Tff7b72val Te6edf3zone Tff7b72= Te6edf3timeZoneTff7b72?.Te6edf3let Tb4b4b4{ Te6edf3runCatching Tb4b4b4{ Te6edf3TimeZoneTb4b4b4.Te6edf3ofTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4) Tb4b4b4} Tff7b72?: Te6edf3TimeZoneTb4b4b4.Te6edf3currentSystemDefaultTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3ClockTb4b4b4.Te6edf3SystemTb4b4b4.Te6edf3todayInTb4b4b4(Te6edf3zoneTb4b4b4) Tff7b72> Te6edf3end
Tb4b4b4}
T8b949e/**
* Parses a `#RRGGBB` brand color into a [Color], or `null` if absent/malformed. Tolerates a missing `#` and any case.
*/
Tff7b72fun Td2a8ffparseBrandColorTb4b4b4(Te6edf3hexTb4b4b4: Tffa657String?Tb4b4b4)Tb4b4b4: Te6edf3Color? Tb4b4b4{
Tff7b72val Te6edf3rgb Tff7b72=
Te6edf3hexTff7b72?.Te6edf3trimTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3removePrefixTb4b4b4(Ta5d6ff"Ta5d6ff#Ta5d6ff"Tb4b4b4)Tff7b72?.Te6edf3takeIf Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3length Tff7b72=Tff7b72= Te6edf3RGB_HEX_LENGTH Tb4b4b4}Tff7b72?.Te6edf3toIntOrNullTb4b4b4(Te6edf3HEX_RADIXTb4b4b4) Tff7b72?: Tff7b72return Tff7b72null
Tff7b72return Te6edf3ColorTb4b4b4(
Te6edf3red Tff7b72= Tb4b4b4(Te6edf3rgb Te6edf3shr Te6edf3RED_SHIFTTb4b4b4) Te6edf3and Te6edf3BYTE_MASKTb4b4b4,
Te6edf3green Tff7b72= Tb4b4b4(Te6edf3rgb Te6edf3shr Te6edf3GREEN_SHIFTTb4b4b4) Te6edf3and Te6edf3BYTE_MASKTb4b4b4,
Te6edf3blue Tff7b72= Te6edf3rgb Te6edf3and Te6edf3BYTE_MASKTb4b4b4,
Tb4b4b4)
Tb4b4b4}
T8b949e/** Parses the edition's `#RRGGBB` [EventFirmwareEdition.accentColor] into a [Color], or `null` if absent/malformed. */
Tff7b72fun Te6edf3EventFirmwareEditionTb4b4b4.Td2a8ffaccentColorOrNullTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Color? Tff7b72= Te6edf3parseBrandColorTb4b4b4(Te6edf3accentColorTb4b4b4)
T8b949e/**
* The edition's full brand palette (`theme.palette`) as colors, malformed entries dropped. Falls back to the named
* `theme.colors` (primary/secondary/accent) and finally the top-level [accentColor][EventFirmwareEdition.accentColor],
* so an edition that carries only one color still yields a usable, non-empty list where one exists.
*/
Tff7b72fun Te6edf3EventFirmwareEditionTb4b4b4.Td2a8ffbrandPaletteTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ColorTff7b72> Tb4b4b4{
T8b949e// distinct() on both paths: a repeated hex would otherwise become a repeated gradient stop, flattening the
T8b949e// gradient over that span. Order is preserved, so the authored reading order still holds.
Tff7b72val Te6edf3fromPalette Tff7b72= Te6edf3themeTff7b72?.Te6edf3paletteTb4b4b4.Te6edf3orEmptyTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3mapNotNullTb4b4b4(Tff7b72::Te6edf3parseBrandColorTb4b4b4)Tb4b4b4.Te6edf3distinctTb4b4b4(Tb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3fromPaletteTb4b4b4.Te6edf3isNotEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72return Te6edf3fromPalette
Tff7b72val Te6edf3named Tff7b72= Te6edf3listOfNotNullTb4b4b4(Te6edf3themeTff7b72?.Te6edf3colorsTff7b72?.Te6edf3primaryTb4b4b4, Te6edf3themeTff7b72?.Te6edf3colorsTff7b72?.Te6edf3secondaryTb4b4b4, Te6edf3themeTff7b72?.Te6edf3colorsTff7b72?.Te6edf3accentTb4b4b4, Te6edf3accentColorTb4b4b4)
Tff7b72return Te6edf3namedTb4b4b4.Te6edf3mapNotNullTb4b4b4(Tff7b72::Te6edf3parseBrandColorTb4b4b4)Tb4b4b4.Te6edf3distinctTb4b4b4(Tb4b4b4)
Tb4b4b4}
T8b949e/**
* The edition's high-contrast detail color — `theme.colors.accent` (DEF CON's pink against its navy primary), falling
* back to `theme.colors.secondary`. Distinct from [accentColorOrNull], which is the *primary* used for the ambient
* wash: the wash wants the calm color, small details want the loud one.
*/
Tff7b72fun Te6edf3EventFirmwareEditionTb4b4b4.Td2a8ffbrandHighlightOrNullTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Color? Tff7b72=
Te6edf3parseBrandColorTb4b4b4(Te6edf3themeTff7b72?.Te6edf3colorsTff7b72?.Te6edf3accentTb4b4b4) Tff7b72?: Te6edf3parseBrandColorTb4b4b4(Te6edf3themeTff7b72?.Te6edf3colorsTff7b72?.Te6edf3secondaryTb4b4b4)
Tff7b72private Tff7b72const Tff7b72val Te6edf3HTTPS_SCHEME Tff7b72= Ta5d6ff"Ta5d6ffhttps://Ta5d6ff"
T8b949e/** A registered name or bracketed IPv6 literal, plus an optional numeric port. See [isSafeBrandUrl]. */
Tff7b72private Tff7b72val Te6edf3AUTHORITY_REGEX Tff7b72= Te6edf3RegexTb4b4b4(Ta5d6ff"""Ta5d6ff(?:[A-Za-z0-9._~-]+|Ta5d6ff\Ta5d6ff[[0-9A-Fa-f:.]+])(?::Ta5d6ff\Ta5d6ffd+)?Ta5d6ff"""Tb4b4b4)
Tff7b72private Tff7b72const Tff7b72val Te6edf3RGB_HEX_LENGTH Tff7b72= T79c0ff6
Tff7b72private Tff7b72const Tff7b72val Te6edf3HEX_RADIX Tff7b72= T79c0ff1T79c0ff6
Tff7b72private Tff7b72const Tff7b72val Te6edf3RED_SHIFT Tff7b72= T79c0ff1T79c0ff6
Tff7b72private Tff7b72const Tff7b72val Te6edf3GREEN_SHIFT Tff7b72= T79c0ff8
Tff7b72private Tff7b72const Tff7b72val Te6edf3BYTE_MASK Tff7b72= T79c0ff0Te6edf3xFF
Served by rngit 1.5.0 - Generated in 0.06s